
package com.be.beadmin.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.design.entity.ProductDrawingInfo;
import com.be.beadmin.design.repository.ProductDrawingInfoRepository;
import com.be.beadmin.product.dto.input.DrawingPrintHstDto;
import com.be.beadmin.product.dto.input.DrawingPrintHstDtoByWoIdAndPage;
import com.be.beadmin.product.dto.output.DrawingPrintHstEx;
import com.be.beadmin.product.entity.DrawingPrintHst;
import com.be.beadmin.product.entity.DrawingPrintHst_;
import com.be.beadmin.product.mapper.DrawingPrintHstMapper;
import com.be.beadmin.product.mapper.DrawingPrintHstMapperEx;
import com.be.beadmin.product.repository.DrawingPrintHstRepository;
import com.be.beadmin.product.service.DrawingPrintHstService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author ljh
* @date 2023-04-03
**/
@Service
@RequiredArgsConstructor
public class DrawingPrintHstServiceImpl implements DrawingPrintHstService {

    @Autowired
    private  DrawingPrintHstRepository drawingPrintHstRepository;
    @Autowired
    private  DrawingPrintHstMapper drawingPrintHstMapper;
    @Autowired
    private  DrawingPrintHstMapperEx drawingPrintHstExMapper;

    @Autowired
    private ProductDrawingInfoRepository productDrawingInfoRepository;

    @Override
    public Page<DrawingPrintHstEx> getDrawingPrintHstByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<DrawingPrintHst> page = drawingPrintHstRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(DrawingPrintHst_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(DrawingPrintHst_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<DrawingPrintHstEx> page1 = page.map(drawingPrintHstExMapper::toEntity);
            return page1;
    }

    @Override
    public Page<DrawingPrintHstEx> getDrawingPrintHstByWoIdAndPage(DrawingPrintHstDtoByWoIdAndPage drawingPrintHstDtoByWoIdAndPage) {
        String workOrderInforId = drawingPrintHstDtoByWoIdAndPage.getWorkOrderInforId();
        if(workOrderInforId==null) throw new BeadminException("未设置工单id-workOrderInforId");
        PageEntity pageEntity = drawingPrintHstDtoByWoIdAndPage.getPageEntity();
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() -1,
                pageEntity.getPageSize());
        Page<DrawingPrintHst> page = drawingPrintHstRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(DrawingPrintHst_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate pre3 = criteriaBuilder.equal(root.get(DrawingPrintHst_.workOrderInforId), workOrderInforId); //过滤为0
                    list.add(pre3);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(DrawingPrintHst_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<DrawingPrintHstEx> page1 = page.map(drawingPrintHstExMapper::toEntity);
        return page1;
    }

    @Override
    public void addDrawingPrintHst(DrawingPrintHstDto drawingPrintHstDto) {
            DrawingPrintHst result = drawingPrintHstRepository.findDrawingPrintHstByIdAndDelete(drawingPrintHstDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("DrawingPrintHst不存在,请重新查询");
            }
            String productDrawingInfoId = drawingPrintHstDto.getProductDrawingInfoId();
            if(productDrawingInfoId==null) throw new BeadminException("productDrawingInfoId未设置");
            String workOrderInforId = drawingPrintHstDto.getWorkOrderInforId();
            if(workOrderInforId==null) throw new BeadminException("工单Id未设置-workOrderInforId");

            ProductDrawingInfo productDrawingInfo = productDrawingInfoRepository.findProductDrawingInfoByIdAndDelete(productDrawingInfoId,0);

            drawingPrintHstDto.setDrawingUrl(productDrawingInfo.getDrawingUrl());
            drawingPrintHstDto.setDrawingDesc(productDrawingInfo.getDrawingName());
            drawingPrintHstDto.setDrawingSequence(productDrawingInfo.getDrawingSequence());

            DrawingPrintHst drawingPrintHst = drawingPrintHstMapper.toEntity(drawingPrintHstDto);
            drawingPrintHstRepository.save(drawingPrintHst);

            productDrawingInfo.setPrintFlag(1);
            productDrawingInfoRepository.save(productDrawingInfo);
    }

    @Override
    public void addDrawingPrintHst2(DrawingPrintHstDto drawingPrintHstDto) {
        DrawingPrintHst result = drawingPrintHstRepository.findDrawingPrintHstByIdAndDelete(drawingPrintHstDto.getId(), 0);
        if(ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("DrawingPrintHst不存在,请重新查询");
        }
        DrawingPrintHst drawingPrintHst = drawingPrintHstMapper.toEntity(drawingPrintHstDto);
        drawingPrintHstRepository.save(drawingPrintHst);
    }

    @Override
    public void updateDrawingPrintHst(DrawingPrintHstDto drawingPrintHstDto) {
            DrawingPrintHst drawingPrintHst = drawingPrintHstRepository.findDrawingPrintHstById(drawingPrintHstDto.getId());
            drawingPrintHst = drawingPrintHstMapper.toEntity(drawingPrintHstDto);
            drawingPrintHstRepository.save(drawingPrintHst);
    }


    @Override
    public void delDrawingPrintHst(String id) {
        DrawingPrintHst drawingPrintHst = drawingPrintHstRepository.findDrawingPrintHstById(id);
        drawingPrintHst.setDelete(1);
        drawingPrintHstRepository.save(drawingPrintHst);
    }

    @Override
    public void delDrawingPrintHsts (List<String> ids) { }


}